package net.wangds.auth.resource.entity;

import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import net.wangds.auth.module.ResourceSchemaEnum;
import net.wangds.auth.module.impls.defaults.DefaultResource;
import net.wangds.data.RemovedEnum;
import net.wangds.data.VisibilityEnum;

import javax.persistence.Entity;
import javax.persistence.Table;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

@Entity
@Table(name = "auth_resource")
@NoArgsConstructor
@SuperBuilder
public class GeneralResourceEntity extends DefaultResource {
    private static final long serialVersionUID = 0;

    /**
     * 资源.
     *
     * @param label    资源名称.
     * @param desc     资源描述.
     * @param schema   业务领域.
     * @param system   系统/应用.
     * @param module   模块.
     * @param feature  特性/菜单/活动.
     * @param activity 活动/动作.
     * @param order    排序权重.
     * @return 资源.
     */
    public static GeneralResourceEntity of(String label, String desc, ResourceSchemaEnum schema,
                                           String system, String module, String feature,
                                           String activity, long order) {
        GeneralResourceEntity res = new GeneralResourceEntity();
        res.setResourceLabel(label);
        res.setResourceDesc(desc);
        res.setActivity(activity);
        res.setFeature(feature);
        res.setModule(module);
        res.setSys(system);
        res.setSchema(schema);
        res.setRemoved(RemovedEnum.NORMAL);

        res.setOrderWeight(order);
        return res;
    }

    /**
     * 动作/行为/操作.
     *
     * @param code        动作代码.
     * @param name        动作名称.
     * @param desc        描述.
     * @param application 系统/应用.
     * @param module      模块.
     * @param activity    活动.
     * @param order       排序权重.
     * @return 动作/行为/操作.
     */
    public static GeneralResourceEntity ofAction(String code, String name, String desc, String application, String module, String activity, long order) {
        return of(name, desc, ResourceSchemaEnum.ACTION, application, module, activity, code, order);
    }

    /**
     * 活动.
     *
     * @param code        活动代码.
     * @param name        活动名称.
     * @param desc        描述.
     * @param application 系统/应用.
     * @param module      模块.
     * @param target      活动url.
     * @param order       排序权重.
     * @return 活动.
     */
    public static GeneralResourceEntity ofActivity(String code, String name, String desc, String application, String module, String target, long order) {
        try {
            return of(name, desc, ResourceSchemaEnum.ACTIVITY, application, module, code, URLEncoder.encode(target, "UTF8"), order);
        } catch (UnsupportedEncodingException e) {
            return null;
        }
    }

    /**
     * 菜单工厂方法.
     *
     * @param menuText   菜单文本.
     * @param icon       图标.
     * @param appCode    应用代码.
     * @param moduleCode 模块代码.
     * @param menuCode   菜单代码.
     * @param target     动作.
     * @param order      排序权重.
     * @return 菜单对象.
     */
    public static GeneralResourceEntity ofMenu(String menuText, String icon, String appCode, String moduleCode, String menuCode,
                                               String target, long order) {
        try {
            return of(menuText, icon, ResourceSchemaEnum.MENU, appCode, moduleCode, menuCode, URLEncoder.encode(target, "UTF8"), order);
        } catch (UnsupportedEncodingException e) {
            return null;
        }
    }

    /**
     * 创建新模块对象.
     *
     * @param moduleCode 模块代码.
     * @param moduleName 模块名称.
     * @param icon       图标.
     * @param desc       描述.
     * @param version    版本.
     * @param target     url.
     * @param order      排序权重.
     * @return 模块.
     */
    public static GeneralResourceEntity ofModule(String moduleCode, String moduleName, String icon, String desc, String version, String target, long order) {
        try {
            return of(moduleName, desc, ResourceSchemaEnum.MODULE, moduleCode, icon, version, URLEncoder.encode(target, "UTF8"), order);
        } catch (UnsupportedEncodingException e) {
            return null;
        }
    }

    /**
     * 应用工厂方法.
     *
     * @param appCode 应用代码.
     * @param appName 应用名称.
     * @param icon    图标.
     * @param desc    描述.
     * @param version 版本.
     * @param target  url.
     * @param order   排序权重.
     * @return 应用.
     */
    public static GeneralResourceEntity ofApplication(String appCode, String appName, String icon, String desc, String version, String target, long order) {
        try {
            return of(appName, desc, ResourceSchemaEnum.APPLICATION, appCode, icon, version, URLEncoder.encode(target, "UTF8"), order);
        } catch (UnsupportedEncodingException e) {
            return null;
        }
    }


    /**
     * 应用工厂方法.
     *
     * @param appCode 应用代码.
     * @param appName 应用名称.
     * @param icon    图标.
     * @param desc    描述.
     * @param version 版本.
     * @param target  url.
     * @param order   排序权重.
     * @param visible 可见性.
     * @return 应用.
     */
    public static GeneralResourceEntity ofApplication(String appCode, String appName, String icon, String desc, String version, String target, long order, VisibilityEnum visible) {
        GeneralResourceEntity res = ofApplication(appCode, appName, icon, desc, version, target, order);
        if (res != null) {
            res.setVisible(visible);
        }
        return res;
    }

    /**
     * 系统工厂方法.
     *
     * @param sysCode 系统代码.
     * @param sysName 系统名称.
     * @param icon    图标.
     * @param desc    描述.
     * @param version 版本.
     * @param url     url.
     * @param order   排序权重.
     * @return 应用.
     */
    public static GeneralResourceEntity ofSystem(String sysCode, String sysName, String icon, String desc, String version, String url, long order) {
        try {
            return of(sysName, desc, ResourceSchemaEnum.SYSTEM, sysCode, icon, version, URLEncoder.encode(url, "UTF8"), order);
        } catch (UnsupportedEncodingException e) {
            return null;
        }
    }

}
